//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    workshopRecommend:{},
    user:'',
    mobile:'',
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  
  //分享
  onShareAppMessage: function (options) {
    return {
      title: '昊信地产 -'+ this.data.userInfo.name+'的名片',
      imageUrl: this.data.userInfo.avatar,
      path: 'pages/index/index?user_id='+this.data.userInfo.user_id
      }
    },
  onLoad: function (options) {
    
    console.log('这里是index')
    console.log(options)
    if (app.globalData.user && app.globalData.user != null) {
    
      //获取用户信息
      this.setData({
        user: app.globalData.user,
        mobile: app.globalData.user.mobile
      })
    } else {
      app.emptyUserCallback = user => {
        if (user != null) {
          this.setData({
            user: app.globalData.user,
            mobile: app.globalData.user.mobile
          });

        }
      }
    }
      
    if(app.globalData.userInfo&&app.globalData.userInfo!=null){
      let user_id = app.globalData.userInfo.user_id
      if (options.scene) {
        user_id = options.scene
      }
      if (options.user_id) {
        user_id = options.user_id
      }
      console.log('用户',user_id)
      if (user_id != app.globalData.userInfo.user_id){
        wx.setStorageSync('user_id', user_id)
        //推荐个人
        wx.request({
          url: app.globalData.apiurl + "agent/" + user_id,
          /*  header: {
              "Authorization": "bearer " + app.globalData.access_token
            },*/
          method: 'get',
          success: res => {
            if (res.data.code == 0) {
              app.globalData.userInfo = res.data.data
              this.setData({
                userInfo: app.globalData.userInfo
              })
            }
          }
        })
      }else{
    
        //获取用户信息
        this.setData({
          userInfo: app.globalData.userInfo
        })
        wx.setNavigationBarTitle({
          title: '昊信地产-' + app.globalData.userInfo.name + '的名片',
        })
      }
     
    
    
    }else{
    
       // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.emptyUserInfoCallback=userInfo=>{
        if (userInfo != null) {
          this.setData({
            userInfo: app.globalData.userInfo
          });
          wx.setNavigationBarTitle({
            title:'昊信地产-'+ app.globalData.userInfo.name + '的名片',
          })
        }
      
      }
    }
    console.log('optiopn',options)
 
   
  
    //推荐房源请求
    wx.request({
      url: app.globalData.apiurl + "workshop_recommend",
    /*  header: {
        "Authorization": "bearer " + app.globalData.access_token
      },*/
      
      method: 'get',
      success: res=> {
        if(res.data.code==0){
            this.setData({
              workshopRecommend:res.data.data
            })
        }
      },
      fail: function () {
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  //地图
  callMap(){
  
      
        let latitude = parseFloat(app.globalData.userInfo.lat)
        let longitude = parseFloat(app.globalData.userInfo.lng)
 
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: latitude,//要去的纬度-地址
          longitude: longitude,//要去的经度-地址
          name: app.globalData.userInfo.address,
          address: app.globalData.userInfo.address
        })
      
  
  
     /*      */
  },
  //电话
  makeCall: function(e){
    console.log(this.data.userInfo.mobile)
    wx.makePhoneCall({
      phoneNumber: this.data.userInfo.mobile
    })
  },
  //跳转投诉看厂页面
  bindtousu: function (e) {
    let url = '/pages/tousu/tousu'
    if (!this.data.mobile) {
      wx.navigateTo({
        url: '/pages/mobile/mobile?url=' + encodeURIComponent(url),
      })
      return;
    }
    wx.navigateTo({
      url: url,
    })
  },
  toHome:function(e){
      wx.redirectTo({
        url: '/pages/home/home',
      })
  },
  //存入通讯录
  addPhone:function(e){
    wx.addPhoneContact({
      photoFilePath: '',
      nickName: '',
      lastName: '',
      middleName: '',
      firstName: this.data.userInfo.name,
      remark: '',
      mobilePhoneNumber: this.data.userInfo.mobile,
      weChatNumber: '',
      addressCountry: '',
      addressState: '',
      addressCity: '',
      addressStreet: '',
      addressPostalCode: '',
      organization: '昊信地产',
      title: '',
      workFaxNumber: '',
      workPhoneNumber: '',
      hostNumber: '',
      email: '',
      url: '',
      workAddressCountry: '',
      workAddressState: '',
      workAddressCity: '',
      workAddressStreet: '',
      workAddressPostalCode: '',
      homeFaxNumber: '',
      homePhoneNumber: '',
      homeAddressCountry: '',
      homeAddressState: '',
      homeAddressCity: '',
      homeAddressStreet: '',
      homeAddressPostalCode: '',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    }) 
  }
  
})
